package com.itheima.exerciseTest.method20_29.method26;

import java.util.ArrayList;

/*
    // 柜台
    private ArrayList al;
    // 柜台存放数量
    private int size;
    // 生产面包的数量
    private int count;
 */
public class Cooker implements Runnable {
    private Storefront sf;
    // 锁
    private static final Object lock = new Object();
    public Cooker(Storefront sf){
        this.sf = sf;
    }

    @Override
    public void run() {
        int count = 0;
        while(true){
            synchronized (sf.getLock()){
                // 判断今天任务完成没，完成了就关门
                if(count >= sf.getCount())
                    break;
                // 判断桌子上是否放了十个面包，放满了就休息
                if(sf.getAl().size() >= 10){
                    try {
                        sf.getLock().wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else{
                    // 没有放满就造
                    ArrayList<String> al = sf.getAl();
                    al.add("面包");
                    sf.setAl(al);
                    count++;
                    sf.setCount(count);
                    System.out.println(Thread.currentThread().getName() +"号厨师做了第" + count + "个面包");
                }
            }
        }
    }
}
